package com.px.scserver.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.px.sccommon.exception.ScException;
import com.px.scserver.mapper.CountDownMapper;
import com.px.scserver.po.CountDown;
import com.px.scserver.service.CountDownService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author 品讯科技
 * @description 针对表【sc_count_down】的数据库操作Service实现
 * @createDate 2023-07-15 21:53:34
 */
@Slf4j
@AllArgsConstructor
@Service
public class CountDownServiceImpl extends ServiceImpl<CountDownMapper, CountDown>
        implements CountDownService {

    @Override
    public Map getCountDownDay() {
        CountDown countDown = getById(1L);
        if (Objects.isNull(countDown)) {
            throw new ScException("请设置考研倒计时！");
        }
        Map map = new HashMap();
        log.info("考研倒计时：{}", countDown.getExaminationTime());
        String year = countDown.getExaminationTime().getYear() + "";
        map.put("year", year);
        Duration duration = Duration.between(LocalDateTime.now(), countDown.getExaminationTime().atStartOfDay());
        map.put("day", Math.toIntExact(duration.toDays()));

        return map;
    }

    @Override
    public LocalDate getCountDownTime() {
        CountDown countDown = getById(1L);
        return countDown.getExaminationTime();
    }

    @Override
    public void setCountDownTime(LocalDate time) {

        if (LocalDate.now().isAfter(time)){
            throw new ScException("请设置大于等于今天的时间！");
        }

        lambdaUpdate()
                .set(CountDown::getExaminationTime, time)
                .eq(CountDown::getId, 1L)
                .update();
    }

}




